from django.db import models
from db.abstract_model import *
from tinymce.models import HTMLField
from utils.wrapper import *



# 故事模型管理器
class StoryManager(models.Manager):

    # 获取最热门三个故事，点赞
    def get_story_hot_3(self):
        try:
            return self.all().order_by("story_praise")[:3]
        except:
            pass

    # 获取所有故事按更新时间倒序排列
    def get_all_stories(self):
        try:
            return self.all().order_by("-update_time")
        except:
            pass

    # 根据id获取一条数据
    def get_one_story(self,story_id):
        try:
            return self.get(id=story_id)
        except:
            pass

    # 更新点赞数

    def update_story_praise(self, request):
        # 根据id获取菜品
        try:
            story_id = post(request, "story_id")
            story = self.get(id=story_id)
            story.story_praise += 1
            story.save()
            return story.story_praise
        except:
            pass

    # 更改故事评论数
    def update_story_response_num(self, request):
        # 根据id获取故事
        story_id = post(request, "story_id")
        story = self.get(id=story_id)
        story.story_response += 1
        story.save()

# 故事模型
class Story(AbstractModel):
    # 故事作者
    story_auther = models.CharField(max_length=20,verbose_name="故事作者")
    # 故事标题
    story_title = models.CharField(max_length=50,verbose_name="故事标题")
    # 故事主体图片
    story_img = models.ImageField(upload_to="images/story/",verbose_name="故事图片")
    # 故事简述
    story_short = models.CharField(max_length=50)
    # 故事内容
    story_desc = HTMLField(verbose_name="故事内容")
    # 故事点赞数
    story_praise = models.IntegerField(default=0,verbose_name="点赞数")
    # 故事评论数
    story_response = models.IntegerField(default=0,verbose_name="评论数")

    objects = StoryManager()

    # admin管理中指定名字
    class Meta:
        verbose_name = "餐厅故事"
        verbose_name_plural = "餐厅故事"


    def __str__(self):
        return self.story_title


# 故事评论模型管理器
class StoryResponseManager(models.Manager):
    # 将评论内容存入数据库
    # 更改故事评论数
    def save_response(self, request):
        user_name = post(request, "user_name")
        response = post(request, "response")
        story_id = post(request, "story_id")
        story_re = StoryResponse()
        story_re.story_id = story_id
        story_re.user_name = user_name
        story_re.response_story = response
        story_re.save()

    # 通过id获取5条评论
    def get_5_response(self, story_id, re_ok_num):
        try:
            return self.filter(story_id=story_id).order_by("-id")[int(re_ok_num):int(re_ok_num) + 5]
        except:
            pass


class StoryResponse(AbstractModel):
    # 评论用户名
    user_name = models.CharField(max_length=20,verbose_name="客户名")
    # 评论
    response_story = models.CharField(max_length=200,verbose_name="故事评论")
    # 评论的故事
    story = models.ForeignKey(Story,verbose_name="评论的故事")

    objects = StoryResponseManager()

    # admin管理中指定名字
    class Meta:
        verbose_name = "餐厅故事评论"
        verbose_name_plural = "餐厅故事评论"

